package com.hyxt.provider;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.hyxt.DTO.PlatLinks;
import com.hyxt.DTO.RespResult;
import com.hyxt.DTO.protocol.*;
import com.hyxt.api.ChainLinkServer;
import com.hyxt.api.ITransferService;
import com.hyxt.utils.Caches;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author songm
 * @version v1.0
 * @Description 数据转发接口实现类
 * @Date: Create in 0:11 2017/12/21
 * @Modifide By:
 **/
@Component
@Service(interfaceClass = ITransferService.class, version = "1.0.1")
public class TransferServiceImpl implements ITransferService {

    //交通部链路管理
    @Reference(version = "1.0.1")
    private ChainLinkServer chainLinkServer2;

    @Override
    public List<PlatLinks> getStatus(String platId) {
        List<PlatLinks> result = new ArrayList<>();
        if (platId != null) {
            PlatLinks platLinks = (PlatLinks) Caches.platLinks.get(platId);
            result.add(platLinks);
        }else {
            /*for(Object value :Caches.platLinks.values()){
                PlatLinks platLinks = (PlatLinks) value;
                result.add((JSONObject) JSON.toJSON(platLinks));
            }*/
            result.addAll((Collection) Caches.platLinks.values());
        }
        return result;
    }

    /**
     * 发送消息
     * @param channelName 发送的通道名
     * @param msg 要发送的消息
     * @return 返回发送状态
     */
    @Override
    public RespResult send(final String channelName, JTBZFBody msg) {

        RespResult result;
        if (channelName != null) {
            if (msg == null) {
                result = new RespResult("2","发送消息不能为空");
                return result;
            }
            switch (msg.getId()){
                //上报运输企业信息(0x11)
                case 0x11 :
                    JTBZF_0x11 data1 = (JTBZF_0x11)msg;
                    byte[] message1 = ProtocolUtil.getMessageByBody((byte) 0x11, data1);
                    //调用链路管理接口
                    chainLinkServer2.sendMsg(channelName,new String(message1));
                    break;
                //删除企业信息(0x12)
                case 0x12 :
                    JTBZF_0x12 data2 = (JTBZF_0x12)msg;
                    byte[] message2 = ProtocolUtil.getMessageByBody((byte) 0x12, data2);
                    //调用链路管理接口
                    chainLinkServer2.sendMsg(channelName,new String(message2));
                    break;
                //上报车辆信息(0x13)
                case 0x13 :
                    JTBZF_0x13 data3 = (JTBZF_0x13) msg;
                    byte[] message3 = ProtocolUtil.getMessageByBody((byte) 0x13, data3);
                    //调用链路管理接口
                    chainLinkServer2.sendMsg(channelName,new String(message3));
                    break;
                //删除车辆信息(0x14)
                case 0x14 :
                    JTBZF_0x14 data4 = (JTBZF_0x14)msg;
                    byte[] message4 = ProtocolUtil.getMessageByBody((byte) 0x14, data4);
                    //调用链路管理接口
                    chainLinkServer2.sendMsg(channelName,new String(message4));
                    break;
                //上报上级平台信息(0x15)
                case 0x15 :
                    JTBZF_0x15 data5 = (JTBZF_0x15)msg;
                    byte[] message5 = ProtocolUtil.getMessageByBody((byte) 0x15, data5);
                    //调用链路管理接口
                    chainLinkServer2.sendMsg(channelName,new String(message5));
                    break;
                //删除上级平台信息(0x16)
                case 0x16 :
                    JTBZF_0x16 data6 = (JTBZF_0x16)msg;
                    byte[] message6 = ProtocolUtil.getMessageByBody((byte) 0x16, data6);
                    //调用链路管理接口
                    chainLinkServer2.sendMsg(channelName,new String(message6));
                    break;
                default:
                    result = new RespResult("3","消息类型错误");
                    return result;
            }
            result = new RespResult("0","发送成功");
            return result;
        }
        result = new RespResult("1","通道名不能为空");
        return result;
    }
}
